Nearest-neighbor chain algorithm